package lohr.lang.dom;

import java.util.Collections;
import java.util.List;

/**
 * A static reference (no methods invoked in the reference, only property references) to a type.
 * May be a qualified reference (that is, more than one node in the reference).
 * May also have an associated name, like a type parameter.
 * May also be parameterized, like a parameterized type.
 * Examples:
 * 
 * Example								# of nodes	name
 * -------								----------	----
 * Ordering.Order.customer 					3		n/a
 * Set<Integer>								1		n/a
 * Set<Integer>								1		n/a
 * Map<K:String,V:Entry>					1		n/a
 * Map<K:String,V:Entry>.Key<String>		2		n/a
 * K:String									1		K
 * 
 * 
 * @author ted stockwell
 *
 */
public class TypeReference extends LohrElement {
	private List<IdentifierName> _nodes;
	private String _name;
	
	public TypeReference(List<IdentifierName> nodes, String name) {
		addChildren(_nodes= Collections.unmodifiableList(nodes));
		_name= name;
	}
	
	/**
	 * Create a named reference
	 */
	public TypeReference(TypeReference reference, IdentifierName name) {
		addChildren(_nodes= reference._nodes);
		if (name != null)
			_name= name.getName();
	}

	public List<IdentifierName> getIdentifierNames() {
		return _nodes;
	}
	
	public String getName() {
		return _name;
	}
	
	@Override
	public String toString() {
		String s= "";
		boolean first= true;
		for (IdentifierName n : _nodes) {
			if (!first)
				s+= ".";
			s+= n.toString();
		}
		if (_name != null)
			s= _name+":"+s;
		return s;
	}
}
